package com.yang.service.adminimpl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketAdmin;
import com.yang.bean.MarketAdminExample;
import com.yang.bean.common.CommonData;
import com.yang.bean.dhr.MarketAdminUpdateVo;
import com.yang.bean.dhr.MarketAdminVo;
import com.yang.bean.dhr.MarketAdminVo1;
import com.yang.mapper.MarketAdminMapper;
import com.yang.service.admin.AdminAdminService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

/**
 * @author dinghongrui
 * @create 2022/09/06 20:41
 */
@Service
public class AdminAdminServiceImpl implements AdminAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;



    @SneakyThrows
    @Override
    public CommonData<MarketAdminVo> queryAllAdmin(Integer page, Integer limit, String sort, String order, String username) {
        PageHelper.startPage(page,limit);
        MarketAdminExample example = new MarketAdminExample();
        example.setOrderByClause(sort + " " + order);
        ObjectMapper objectMapper = new ObjectMapper();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        /*if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }*/
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        List<MarketAdminVo> marketAdminVos = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {
            MarketAdminVo marketAdminVo = new MarketAdminVo();
            marketAdminVo.setId(marketAdmin.getId());
            marketAdminVo.setUsername(marketAdmin.getUsername());
            marketAdminVo.setAvatar(marketAdmin.getAvatar());
            marketAdminVo.setRoleIds(objectMapper.readValue(marketAdmin.getRoleIds(),Integer[].class));
            marketAdminVos.add(marketAdminVo);
        }
        PageInfo pageInfo = new PageInfo(marketAdmins);
//        CommonData data = CommonData.data(pageInfo);
        CommonData<MarketAdminVo> data = new CommonData<>();
        data.setTotal((int) pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setLimit(pageInfo.getPageSize());
        data.setPage(pageInfo.getPageNum());
        data.setList(marketAdminVos);
        return data;

    }

    @SneakyThrows
    @Override
    public MarketAdminVo1 createAdmin(MarketAdminVo1 marketAdminVo1) {
        Date date = new Date();
        marketAdminVo1.setAddTime(date);
        marketAdminVo1.setUpdateTime(date);
        MarketAdmin marketAdmin = new MarketAdmin();
        ObjectMapper objectMapper = new ObjectMapper();
        marketAdmin.setUsername(marketAdminVo1.getUsername());
        marketAdmin.setPassword(marketAdminVo1.getPassword());
        marketAdmin.setAvatar(marketAdminVo1.getAvatar());
        marketAdmin.setRoleIds(objectMapper.writeValueAsString(marketAdminVo1.getRoleIds()));
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        marketAdminMapper.insertSelective(marketAdmin);
        marketAdminVo1.setId(marketAdmin.getId());
        return marketAdminVo1;
    }

    @SneakyThrows
    @Override
    public MarketAdminUpdateVo updateAdmin(MarketAdminVo1 marketAdminVo1) {
        Date date = new Date();
        ObjectMapper objectMapper = new ObjectMapper();
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(marketAdminVo1.getId());
        marketAdmin.setUsername(marketAdminVo1.getUsername());
        marketAdmin.setPassword(marketAdminVo1.getPassword());
        marketAdmin.setAvatar(marketAdminVo1.getAvatar());
        marketAdmin.setUpdateTime(date);
        marketAdmin.setRoleIds(objectMapper.writeValueAsString(marketAdminVo1.getRoleIds()));
        marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);

        MarketAdminUpdateVo updateVo = new MarketAdminUpdateVo();
        updateVo.setId(marketAdminVo1.getId());
        updateVo.setUsername(marketAdminVo1.getUsername());
        updateVo.setAvatar(marketAdminVo1.getAvatar());
        updateVo.setUpdateTime(date);
        updateVo.setRoleIds(marketAdminVo1.getRoleIds());
        return updateVo;

    }

    @SneakyThrows
    @Override
    public void deleteAdmin(MarketAdminVo1 marketAdminVo1) {
        ObjectMapper objectMapper = new ObjectMapper();
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(marketAdminVo1.getId());
        marketAdmin.setUsername(marketAdmin.getUsername());
        marketAdmin.setPassword(marketAdmin.getPassword());
        marketAdmin.setAvatar(marketAdminVo1.getAvatar());
        marketAdmin.setAddTime(marketAdminVo1.getAddTime());
        marketAdmin.setDeleted(true);
        marketAdmin.setRoleIds(objectMapper.writeValueAsString(marketAdminVo1.getRoleIds()));
        marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
//        marketAdminMapper.deleteByPrimaryKey(marketAdmin.getId());
    }
}
